/**
 * Created by Administrator on 2217/5/14.
 */
var map = new BMap.Map("map");  // 创建Map实例
var point = new BMap.Point(119.014521, 25.459865);// 初始化地图,用城市名设置地图中心点
map.centerAndZoom(point, 15);//
map.enableScrollWheelZoom();//地图可以缩放
map.enableContinuousZoom();

var blist = [];
var districtLoading = 0;
function getBoundary() {
    addDistrict("莆田市");

}
/**
 * 添加行政区划
 * @param {} districtName 行政区划名
 * @returns  无返回值
 */
function addDistrict(districtName) {
    //使用计数器来控制加载过程
    districtLoading++;
    var bdary = new BMap.Boundary();
    bdary.get(districtName, function (rs) {       //获取行政区域
        var count = rs.boundaries.length; //行政区域的点有多少个
        if (count === 0) {
            alert('未能获取当前输入行政区域');
            return;
        }
        for (var i = 0; i < count; i++) {
            blist.push({ points: rs.boundaries[i], name: districtName });
        };
        //加载完成区域点后计数器-1
        districtLoading--;
        if (districtLoading == 0) {
            //全加载完成后画端点
            drawBoundary();
        }
    });
}

function drawBoundary() {
    //包含所有区域的点数组
    var pointArray = [];
    var pNW = { lat: 59.0, lng: 73.0 };
    var pNE = { lat: 59.0, lng: 136.0 };
    var pSE = { lat: 3.0, lng: 136.0 };
    var pSW = { lat: 3.0, lng: 73.0 };
    var pArray = [];
    pArray.push(pNW);
    pArray.push(pSW);
    pArray.push(pSE);
    pArray.push(pNE);
    pArray.push(pNW);
    //循环添加各闭合区域
    for (var i = 0; i < blist.length; i++) {
        //添加显示用标签层
        var label = new BMap.Label(blist[i].name, { offset: new BMap.Size(20, -10) });
        label.hide();
        map.addOverlay(label);

        //添加多边形层并显示
        var ply = new BMap.Polygon(blist[i].points, { strokeWeight: 1, strokeColor: "#FF0000", fillOpacity: 0.01, fillColor: " #FFFFFF" }); //建立多边形覆盖物
        ply.name = blist[i].name;
        ply.label = label;
        map.addOverlay(ply);


        //将点增加到视野范围内
        var path = ply.getPath();
        pointArray = pointArray.concat(path);
        //将闭合区域加到遮蔽层上，每次添加完后要再加一次西北角作为下次添加的起点和最后一次的终点
        pArray = pArray.concat(path);
        pArray.push(pArray[0]);
    }

    //限定显示区域，需要引用api库
    var boundply = new BMap.Polygon(pointArray);
    BMapLib.AreaRestriction.setBounds(map, boundply.getBounds());
    map.setViewport(pointArray);    //调整视野
    //添加遮蔽层
    var plyall = new BMap.Polygon(pArray, { strokeOpacity: 0.0000001, strokeColor: "#000000", strokeWeight: 0.00001, fillColor: "#000000", fillOpacity:1 }); //建立多边形覆盖物
    map.addOverlay(plyall);
}

getBoundary();
/************************************************************声明全局变量的标注marker***********************************/
var senseMarker,
    buildMarker,
    coverMarker,
    hanMarker,
    dtuMarker,
    valveMarker,
    eventMarker,
    peopleMarker,
    pipeMarker,
    polyline;

/************************************************************设施方法*************************************/
/**查询全部设施**/
function queryDevice() {
    querySense();
    queryBuild();
    queryCover();
    queryHan();
    querySenseDev();
    queryValve();
}

queryDevice();
//定时器，timeFun()方法去判断设施为active属性时，去刷新dtu设备的数据
function timeFun() {
    var name = document.getElementById("facilitiesBtn").className;
    if (name== "active") {
        setInterval('querySenseDev()', 15000); //指定1秒刷新一次
    }else{
        return
    }
}
setInterval('timeFun()', 15000);
/************************************************************传感方法*************************************/
/**查询全部传感**/
function querySense() {
    $.ajax({
        type: "GET",
        url: "/rest/web/index/selectSenseList",
        dataType: "json",
        success: function (data) {
            var senseList = data.msg;
            $.each(senseList, function (i, o) {
                setSensePoint(o);
            });
        }
    });
}

/**
 * 绘制传感点
 * @param obj
 */
function setSensePoint(obj) {
    var senseContent = "";
    senseContent = senseContent + "<div class='senseContent' id='senseDemo'>" +
        "<div><h6>类别：</h6><p>" + obj.type + "</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.sno + "</p></div>" +
        "<div><h6>年份：</h6><p>" + (obj.year) + "</p></div>" +
        "<div><h6>规格：</h6><p>" + obj.spec + "</p></div>" +
        "<div><h6>材质：</h6><p>" + obj.quality + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + obj.age + "</p></div>" +
        "<div><h6>责任：</h6><p>" + obj.personLiable + "</p></div>" +
        "<div><h6>部门：</h6><p>" + obj.department + "</p></div>" +
        "<div class='remarks' ><h6>备注：</h6><p>" + obj.remarks + "</p></div>" +
        "</div>" +
        "</div>";

    var senseWindow = new BMap.InfoWindow(senseContent); // 创建信息窗口对象
    //创建传感小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_sensor.png", new BMap.Size(22, 22));
    senseMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    senseMarker.parent = "deviceName";
    map.addOverlay(senseMarker);
    senseMarker.addEventListener("click", function () {
        this.openInfoWindow(senseWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('senseDemo').onload = function () {
            senseWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/************************************************************建筑方法*************************************/
/**查询全部建筑**/
function queryBuild() {
    $.ajax({
        type: "GET",
        url: "/rest/web/index/selectArchitectureList",
        dataType: "json",
        success: function (data) {
            var buildList = data.data;
            $.each(buildList, function (i, o) {
                setBuildPoint(o);
            });
        }
    });
}

function setBuildPoint(obj) {
    var buildContent = "";
    buildContent = buildContent + "<div class='senseContent' id='buildDemo'>" +
        "<div><h6>类别：</h6><p>" + obj.type + "</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.sno + "</p></div>" +
        "<div><h6>年份：</h6><p>" + obj.year + "</p></div>" +
        "<div><h6>规格：</h6><p>" + obj.spec + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + obj.age + "</p></div>" +
        "<div><h6>责任：</h6><p>" + obj.personLiable + "</p></div>" +
        "<div><h6>部门：</h6><p>" + obj.department + "</p></div>" +
        "<div class='remarks'><h6>备注：</h6><p>" + obj.remarks + "</p></div>" +
        "</div>";

    var buildWindow = new BMap.InfoWindow(buildContent); // 创建信息窗口对象
    //创建建筑小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_building.png", new BMap.Size(22, 22));
    buildMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    buildMarker.parent = "deviceName";
    map.addOverlay(buildMarker);
    buildMarker.addEventListener("click", function () {
        this.openInfoWindow(buildWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('buildDemo').onload = function () {
            buildWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/************************************************************井盖方法*************************************/
/**查询全部井盖**/
function queryCover() {
    $.ajax({
        type: "GET",
        url: "/rest/web/index/selectManholeList ",
        dataType: "json",
        success: function (data) {
            var coverList = data.data;
            $.each(coverList, function (i, o) {
                setCoverPoint(o);
            });
        }
    });
}

/**
 * 绘制井盖的点
 * @param obj
 */
function setCoverPoint(obj) {
    var coverContent = "";
    coverContent = coverContent + "<div class='senseContent' id='coverDemo'>" +
        "<div><h6>类别：</h6><p>" + obj.type + "</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.sno + "</p></div>" +
        "<div><h6>年份：</h6><p>" + obj.year + "</p></div>" +
        "<div><h6>规格：</h6><p>" + obj.spec + "</p></div>" +
        "<div><h6>井深：</h6><p>" + obj.depth + "</p></div>" +
        "<div><h6>防护：</h6><p>" + obj.protect + "</p></div>" +
        "<div><h6>爬梯：</h6><p>" + obj.ladder + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + obj.age + "</p></div>" +
        "<div><h6>材质：</h6><p>" + obj.quality + "</p></div>" +
        "<div><h6>形状：</h6><p>" + obj.shape + "</p></div>" +
        "<div><h6>责任：</h6><p>" + obj.personLiable + "</p></div>" +
        "<div><h6>部门：</h6><p>" + obj.department + "</p></div>" +
        "<div class='remarks'><h6>备注：</h6><p>" + obj.remarks + "</p></div>" +
        "</div>";
    var coverWindow = new BMap.InfoWindow(coverContent); // 创建信息窗口对象
    //创建井盖小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_covers.png", new BMap.Size(22, 22));
    coverMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    coverMarker.parent = "deviceName";
    map.addOverlay(coverMarker);
    coverMarker.addEventListener("click", function () {
        this.openInfoWindow(coverWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('coverDemo').onload = function () {
            coverWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/************************************************************涵匣方法*************************************/
/**创建涵匣的全部查询 **/
function queryHan() {
    $.ajax({
        type: "GET",
        url: "/rest/web/information/selectGatesList",
        dataType: "json",
        success: function (data) {
            var hanList = data.data;
            $.each(hanList, function (i, o) {
                setHanPoint(o);
            });
        }
    });
}

/**
 * 绘制涵匣点
 * @param obj
 */
function setHanPoint(obj) {
    var hanContent = "";
    hanContent = hanContent + "<div class='senseContent' id='hanDemo'>" +
        "<div><h6>类别：</h6><p>" + obj.type + "</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.sno + "</p></div>" +
        "<div><h6>年份：</h6><p>" + obj.year + "</p></div>" +
        "<div><h6>规格：</h6><p>" + obj.spec + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + obj.age + "</p></div>" +
        "<div><h6>责任：</h6><p>" + obj.personLiable + "</p></div>" +
        "<div><h6>部门：</h6><p>" + obj.department + "</p></div>" +
        "<div class='remarks'><h6>备注：</h6><p>" + obj.remarks + "</p></div>" +
        "</div>";
    var hanWindow = new BMap.InfoWindow(hanContent); // 创建信息窗口对象
    //创建涵匣小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_hanza.png", new BMap.Size(22, 22));
    hanMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    hanMarker.parent = "deviceName";
    map.addOverlay(hanMarker);
    hanMarker.addEventListener("click", function () {
        this.openInfoWindow(hanWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('hanDemo').onload = function () {
            hanWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/************************************************************传感设备方法*************************************/
/**创建传感设备的全部查询 **/
function querySenseDev() {
    $.ajax({
        type: "GET",
        url: "/rest/web/device/selectDeviceList",
        dataType: "json",
        success: function (data) {
            var dtuList = data.data;
            $.each(dtuList, function (i, o) {
                setSenseDevPoint(o);
            });
        }
    });
}

function setSenseDevPoint(obj) {
    var dtuContent =
        "<div class='senseContent' id='dtuDemo'>" +
        "<div><h6>类别：</h6><p>DTU设备</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.code + "</p></div>" +
        "<div><h6>年份：</h6><p>" + (obj.year == null ? " " : obj.year) + "</p></div>" +
        "<div><h6>规格：</h6><p>" + (obj.spec == null ? " " : obj.spec) + "</p></div>" +
        "<div><h6>材质：</h6><p>" + (obj.quality == null ? " " : obj.quality) + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + (obj.age == null ? " " : obj.age) + "</p></div>" +
        "<div><h6>责任：</h6><p>" + (obj.personLiable == null ? " " : obj.personLiable) + "</p></div>" +
        "<div><h6>部门：</h6><p>" + (obj.department == null ? " " : obj.department) + "</p></div>" +
        "<div><h6>流量(m3/h)：</h6><p>" + (obj.curFlow == null ? " " : obj.curFlow) + "</p></div>" +
        "<div><h6>流速(m/s)：</h6><p>" + (obj.curSpeed == null ? " " : obj.curSpeed) + "</p></div>" +
        "<div><h6>正向总量(m3)：</h6><p>" + (obj.curTotalPositive == null ? " " : obj.curTotalPositive) + "</p></div>" +
        "<div><h6>备注：</h6><p>" + obj.remark + "</p></div>" +
        "</div>" +
        "</div>";
    //传感设备的文本显示框
    var dtuShowContent =
        "<div class='dtuShowContent'>" +
        "<div><span></span><p>" + obj.name + "</p></div>" +
        "<div><span></span><p>流量(m3/h):&nbsp;&nbsp;" + (obj.curFlow == null ? " " : obj.curFlow) + "</p></div>" +
        "</div>";
    var dtuWindow = new BMap.InfoWindow(dtuContent); // 创建信息窗口对象
    //创建传感的设备小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    //判断是否开启报警，是的情况下去判断报警阀值上限超过流量或者报警阀值下限小于等于0的情况下，小图标变成红色
    var myIcon;
    if (obj.isSysAlarmOn == 1) {
        if (obj.curFlow<obj.alarmThresholdLowerLimit || obj.curFlow>obj.alarmThresholdUpperLimit) {
            myIcon = new BMap.Icon("/static/images/map_dtu1.png", new BMap.Size(22, 22));
        } else {
            myIcon = new BMap.Icon("/static/images/map_dtu.png", new BMap.Size(22, 22));
        }
    } else {
        myIcon = new BMap.Icon("/static/images/map_dtu.png", new BMap.Size(22, 22));
    }
    dtuMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    dtuMarker.parent = "deviceName";
    map.addOverlay(dtuMarker);
    var label = new BMap.Label(dtuShowContent, {offset: new BMap.Size(-57, -67)});
    dtuMarker.setLabel(label);
    dtuMarker.addEventListener("click", function () {
        this.openInfoWindow(dtuWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('dtuDemo').onload = function () {
            dtuWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/************************************************************阀门方法*************************************/
/**创建阀门的全部查询 **/
function queryValve() {
    $.ajax({
        type: "GET",
        url: "/rest/web/valve/selectValveList",
        dataType: "json",
        success: function (data) {
            var valveList = data.data;
            $.each(valveList, function (i, o) {
                setValvePoint(o);
            });
        }
    });
}

function setValvePoint(obj) {
    var valveContent =
        "<div class='senseContent' id='valveDemo'>" +
        "<div><h6>类别：</h6><p>" + obj.type + "</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.sno + "</p></div>" +
        "<div><h6>年份：</h6><p>" + obj.year + "</p></div>" +
        "<div><h6>规格：</h6><p>" + obj.spec + "</p></div>" +
        "<div><h6>井深：</h6><p>" + obj.depth + "</p></div>" +
        "<div><h6>防护：</h6><p>" + obj.protect + "</p></div>" +
        "<div><h6>爬梯：</h6><p>" + obj.ladder + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + obj.age + "</p></div>" +
        "<div><h6>材质：</h6><p>" + obj.quality + "</p></div>" +
        "<div><h6>形状：</h6><p>" + obj.shape + "</p></div>" +
        "<div><h6>责任：</h6><p>" + obj.personLiable + "</p></div>" +
        "<div><h6>部门：</h6><p>" + obj.department + "</p></div>" +
        "<div class='remarks'><h6>备注：</h6><p>" + obj.remarks + "</p></div>" +
        "</div>";
    var valveWindow = new BMap.InfoWindow(valveContent); // 创建信息窗口对象
    //创建阀门小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_valve.png", new BMap.Size(22, 22));
    valveMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    valveMarker.parent = "deviceName";
    map.addOverlay(valveMarker);
    valveMarker.addEventListener("click", function () {
        this.openInfoWindow(valveWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('valveDemo').onload = function () {
            valveWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/*******************************点击设备按钮出现相应的数据*************************************/
var deviceKey = 2;
$("#facilitiesBtn").click(function () {
    if (deviceKey == 1) {
        $(this).addClass('active');
        queryDevice();
        deviceKey = 2;
    } else {
        $(this).removeClass('active');
        delDeviceMarker();
        deviceKey = 1;
    }
});

function delDeviceMarker() {
    var alloverlay = window.map.getOverlays();
    for (var i in alloverlay) {//通过遍历删除地图上的标注
        if (alloverlay[i].parent == 'deviceName') {
            window.map.removeOverlay(alloverlay[i]);
        }
    }
}

/*********************************************************管网方法***************************************/
/**创建管网的全部查询 **/
function queryPipe() {
    $.ajax({
        type: "GET",
        url: "/rest/web/information/selectPipeList",
        dataType: "json",
        success: function (data) {
            var pipeList = data.data;
            $.each(pipeList, function (i, o) {
                setPipePoint(o);
            });
        }
    });
}

/**
 * 绘制管网点
 * @param obj
 */
function setPipePoint(obj) {
    var pipeContent = "";
    pipeContent = pipeContent + "<div class='senseContent'>" +
        "<div><h6>类别：</h6><p>" + obj.type + "</p></div>" +
        "<div><h6>名称：</h6><p>" + obj.name + "</p></div>" +
        "<div><h6>编号：</h6><p>" + obj.sno + "</p></div>" +
        "<div><h6>年份：</h6><p>" + obj.year + "</p></div>" +
        "<div><h6>规格：</h6><p>" + obj.spec + "</p></div>" +
        "<div><h6>埋深：</h6><p>" + obj.depth + "</p></div>" +
        "<div><h6>高程：</h6><p>" + obj.altitude + "</p></div>" +
        "<div><h6>壁厚：</h6><p>" + obj.thickness + "</p></div>" +
        "<div><h6>寿命：</h6><p>" + obj.age + "</p></div>" +
        "<div><h6>材质：</h6><p>" + obj.quality + "</p></div>" +
        "<div><h6>责任：</h6><p>" + obj.personLiable + "</p></div>" +
        "<div><h6>部门：</h6><p>" + obj.department + "</p></div>" +
        "<div class='remarks'><h6>备注：</h6><p>" + obj.remarks + "</p></div>" +
        "</div>";
    var pipeWindow = new BMap.InfoWindow(pipeContent);
    var startPt = new BMap.Point(obj.startLng, obj.startLat);
    var endPt = new BMap.Point(obj.endLng, obj.endLat);
    polyline = new BMap.Polyline([
        startPt,
        endPt
    ], {strokeColor: "blue", strokeWeight: 2});
    polyline.parentLine = 'pipeline';
    map.addOverlay(polyline);
    var myIcon = new BMap.Icon("/static/images/map_pipe.png", new BMap.Size(22, 22));
    pipeMarker = new BMap.Marker(endPt, {icon: myIcon}); // 创建标注
    map.centerAndZoom(endPt, 15);
    pipeMarker.parent = 'pipeName';
    map.addOverlay(pipeMarker);
    pipeMarker.addEventListener("click", function () {
        this.openInfoWindow(pipeWindow);
    });
}

/*******************************点击官网按钮出现相应的数据*************************************/
var pipeKey = 1;
$("#networkBtn").click(function () {
    if (pipeKey == 1) {
        $(this).addClass('active');
        queryPipe();
        pipeKey = 2;
    } else {
        $(this).removeClass('active');
        delPipeMarker(pipeMarker, polyline);
        pipeKey = 1;
    }
});

function delPipeMarker(pipeMarker, polyline) {
    var parent = pipeMarker.parent,
        parentLine = polyline.parentLine;
    var alloverlay = window.map.getOverlays();
    for (var i in alloverlay) {//通过遍历删除地图上的标注
        if (alloverlay[i].parent == 'pipeName' || alloverlay[i].parentLine == 'pipeline') {
            window.map.removeOverlay(alloverlay[i]);

        }
    }
}

/*********************************************************危险方法**********************************************/
/**查询全部危险**/
function queryDanger() {
    $.ajax({
        type: "GET",
        url: "/rest/web/index/selectDangerList",
        dataType: "json",
        data: {
            status: 0
        },
        success: function (data) {
            var dangerList = data.data;
            $.each(dangerList, function (i, o) {
                setDangerPoint(o);
            });
        }
    });
}

/**
 * 绘制点危险
 * @param obj
 */
function setDangerPoint(obj) {
    var dangerContent = "";
    dangerContent = dangerContent + "<div class='second-danger' id=''dangerDemo'>" +
        "<div><h6>危险主题：</h6><p>" + obj.title + "</p></div>" +
        "<div><h6>危险地点：</h6><p>" + obj.address + "</p></div>" +
        "<div><h6>更新时间：</h6><p>" + obj.createTime+ "</p></div>" +
        "<div class='remarks'><h6>最新进度：</h6><p>" + obj.content + "</p></div>" +
        "</div>";
    var opts = {
        width: 280,     // 信息窗口宽度
        height: 130     // 信息窗口高度
    };
    var dangerWindow = new BMap.InfoWindow(dangerContent, opts); // 创建信息窗口对象
    //创建危险小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_danger.png", new BMap.Size(22, 22));
    dangerMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    dangerMarker.parent = 'dangerName';
    map.addOverlay(dangerMarker);
    dangerMarker.addEventListener("click", function () {
        this.openInfoWindow(dangerWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('dangerDemo').onload = function () {
            dangerWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/*******************************点击危险按钮出现相应的数据*************************************/
var dangerKey = 1;
$("#dangerBtn").click(function () {
    if (dangerKey == 1) {
        $(this).addClass('active');
        queryDanger();
        dangerKey = 2;
    } else {
        $(this).removeClass('active');
        delDangerMarker(dangerMarker);
        dangerKey = 1;
    }
});

function delDangerMarker(dangerMarker) {
    var parent = dangerMarker.parent;
    var alloverlay = window.map.getOverlays();
    for (var i in alloverlay) {//通过遍历删除地图上的标注
        if (alloverlay[i].parent == 'dangerName' && alloverlay[i].toString() == "[object Marker]") {
            window.map.removeOverlay(alloverlay[i]);

        }
    }
}

/*********************************************************人员方法*****************************************/
/**查询全部人员**/
function queryPeople() {
    $.ajax({
        type: "GET",
        url: "/rest/web/locationLog/selectAll",
        dataType: "json",
        data: {
            createTime: new Date(new Date()).Format("yyyy-MM-dd")
        },
        success: function (data) {
            var peopleList = data.data;
            $.each(peopleList, function (i, o) {
                if (o.createTime != null) {
                    setPeoplePoint(o);
                }

            });
        }
    });
}

/**
 * 绘制点人员
 * @param obj
 */
function setPeoplePoint(obj) {
    var peopleContent = "";
    var status = (obj.createTime == null ? "离线" : "在线");
    peopleContent = peopleContent + "<div class='peopleContent  senseContent ' id='peopleDemo'>" +
        "<div><h5>" + obj.nickName + "&nbsp; </h5><p>" + obj.department + " &nbsp; &nbsp;  状态：<span>" + status + "</span></p></div>" +
        "<div><h6>当前位置：</h6><p>" + obj.address + "</p></div>" +
        "<div><h6>最近在线：</h6><p>" + obj.createTime + "</p></div>" +
        "<div><h6>手机号码：</h6><p>" + obj.phone + "</p></div>" +
        "<div><h6>坐标：</h6><p class='coordinate'>" + obj.lng + "," + obj.lat + "</p></div>" +
        "</div>";
    var opts = {
        width: 280,     // 信息窗口宽度
        height: 120     // 信息窗口高度
    };
    var peopleWindow = new BMap.InfoWindow(peopleContent, opts); // 创建信息窗口对象
    //创建人员小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_people.png", new BMap.Size(22, 22));
    peopleMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    peopleMarker.parent = 'peopleName';
    map.addOverlay(peopleMarker);
    peopleMarker.addEventListener("click", function () {
        this.openInfoWindow(peopleWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('peopleDemo').onload = function () {
            peopleWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/*******************************点击人员按钮出现相应的数据*************************************/
var peopleKey = 1;
$("#peopleBtn").click(function () {
    if (peopleKey == 1) {
        $(this).addClass('active');
        queryPeople();
        peopleKey = 2;

    } else {
        $(this).removeClass('active');
        delPeopleMarker(peopleMarker);
        peopleKey = 1;
    }
});

function delPeopleMarker(peopleMarker) {
    var parent = peopleMarker.parent;
    var alloverlay = window.map.getOverlays();
    for (var i in alloverlay) {//通过遍历删除地图上的标注
        if (alloverlay[i].parent == 'peopleName' && alloverlay[i].toString() == "[object Marker]") {
            window.map.removeOverlay(alloverlay[i]);

        }
    }
}

/*********************************************************事件方法*********************************************/
/**查询全部事件**/
function queryEvent() {
    $.ajax({
        type: "GET",
        url: "/rest/web/index/selectEventInfoList",
        dataType: "json",
        data: {
            status: 0
        },
        success: function (data) {
            var eventList = data.data;
            $.each(eventList, function (i, o) {
                setEventPoint(o);
            });
        }
    });
}

/**
 * 绘制点事件
 * @param obj
 */

function setEventPoint(obj) {
    var eventContent = "";
    eventContent = eventContent + "<div class='second-danger' id='eventDemo'>" +
        "<div><h6>上报人：</h6><p>" + obj.nickName + "</p></div>" +
        "<div><h6>上报时间：</h6><p>" + (new Date(obj.createTime).Format("yyyy-MM-dd hh:mm:ss")) + "</p></div>" +
        "<div><h6>事件主题：</h6><p>" + obj.title + "</p></div>" +
        "<div><h6>事件地点：</h6><p>" + obj.address + "</p></div>" +
        "</div>";
    var opts = {
        width: 280,     // 信息窗口宽度
        height: 90     // 信息窗口高度
    };
    var eventWindow = new BMap.InfoWindow(eventContent, opts); // 创建信息窗口对象

    //创建事件小图标
    var pt = new BMap.Point(obj.lng, obj.lat);
    var myIcon = new BMap.Icon("/static/images/map_event.png", new BMap.Size(22, 22));
    eventMarker = new BMap.Marker(pt, {icon: myIcon}); // 创建标注
    eventMarker.parent = 'eventName';
    map.addOverlay(eventMarker);
    eventMarker.addEventListener("click", function () {
        this.openInfoWindow(eventWindow);
        //图片加载完毕重绘infowindow
        document.getElementById('eventDemo').onload = function () {
            eventWindow.redraw(); //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
        }
    });
}

/*******************************点击事件按钮出现相应的数据*************************************/
var key = 1;
$("#eventBtn").click(function () {
    if (key == 1) {
        $(this).addClass('active');
        queryEvent();
        key = 2;

    } else {
        $(this).removeClass('active');
        delEventMarker(eventMarker);
        key = 1;
    }
});

function delEventMarker(eventMarker) {
    var parent = eventMarker.parent;
    var alloverlay = window.map.getOverlays();
    for (var i in alloverlay) {//通过遍历删除地图上的标注
        if (alloverlay[i].parent == 'eventName' && alloverlay[i].toString() == "[object Marker]") {
            window.map.removeOverlay(alloverlay[i]);

        }
    }
}